<?php

  /**
   * Comments
   *
   * Класс виджета, выводящего комменты
   */

  class Comments extends XMLWidget
  {
    /**
     * instance, primary key or list
     *
     * @var mixed
     */
    public $comments = null;

    /**
     * запустить виджет
     *
     * @return void
     * @access public
     */
    public function run()
    {
      if (is_array($this->comments))
        $isList = !empty($this->comments);
      else
      {
        if ($this->comments && !($this->comments instanceof Comment))
          $this->comments = Comment::model()->findByPk($this->comments);
        $this->comments = $this->comments ? array($this->comments) : array();
        $isList         = false;
      }

      if ($isList)
        self::openNode("comments");

      /** @var $c XMLController */
      $c = $this->controller;

      /** @var $comment Comment */
      foreach ($this->comments as $comment)
        if (!$comment->is_sentenced)
        {
          /** @var $post Post */
          $post  = $comment->originPost;
          $attrs = array(
            "id"         => $comment->id,
            "can-remove" => intval($c->checkAccess("remove_comment", false, array("comment" => $comment))),
            "date-time"  => $c->app->frmDate($comment->stamp, false),
            "post-id"    => $post->id,
            "post-autor" => $post->user->id
          );
          if ($isList)
            $attrs["score"] = Authority::model()->userScore($comment->id, Comment::COMMENTS);

          self::openNode("comment", $attrs);

          /** @var $autor User */
          if ($autor = $comment->user)
            self::autorNode($autor);

          self::node("content", $comment->comment, array("cdata" => true));
          $c->widget("Comments", array("comments" => $comment->commentaries));

          self::closeNode("comment");
        }

      if ($isList)
        self::closeNode("comments");
    }
  }

?>
